/*
 * Copyright 2010 Gal Dolber.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.unnison.demo.client.main;

import com.google.gwt.dom.client.Style.Visibility;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

import com.unnison.demo.client.attacher.RootLayoutPanelAttacher;
import com.unnison.demo.client.blippanel.BlipPanel;
import com.unnison.demo.client.common.HasInnerHTML;
import com.unnison.demo.client.common.HasWidget;
import com.unnison.demo.client.complexplace.ComplexPlaceData;
import com.unnison.demo.client.complexplace.ComplexPlacePresenter;
import com.unnison.demo.client.dynamic.DynamicPresenter;
import com.unnison.demo.client.jsonserializer.JsonSerializerPresenter;
import com.unnison.demo.client.login.LoginPresenter;
import com.unnison.demo.client.main.MainPresenterImpl.MainBinder;
import com.unnison.demo.client.microevents.MicroeventsPresenter;
import com.unnison.demo.client.styler.Styler;
import com.unnison.framework.client.GuitPresenter;
import com.unnison.framework.client.View;
import com.unnison.framework.client.async.event.AsyncActivityEvent;
import com.unnison.framework.client.async.event.AsyncActivityType;
import com.unnison.framework.client.binder.EventBusHandler;
import com.unnison.framework.client.binder.GuitBinder;
import com.unnison.framework.client.binder.ViewField;
import com.unnison.framework.client.binder.ViewHandler;
import com.unnison.framework.client.binder.ViewInitializer;

import java.util.LinkedHashMap;
import java.util.Map.Entry;

public class MainPresenterImpl extends GuitPresenter<MainBinder> implements MainPresenter {

    public interface MainBinder extends GuitBinder<MainPresenterImpl> {
    }

    private final RootLayoutPanelAttacher attacher;

    @ViewField
    public HasInnerHTML navegation;

    @ViewField
    public HasWidget panel;

    @ViewField
    Styler loading;

    LinkedHashMap<String, String> places = new LinkedHashMap<String, String>();

    int asyncCount = 0;

    @Inject
    public MainPresenterImpl(RootLayoutPanelAttacher attacher) {
        this.attacher = attacher;
    }

    @EventBusHandler(AsyncActivityEvent.class)
    protected void $asyncActivity(AsyncActivityType activityType) {
        switch (activityType) {
            case START:
                asyncCount++;
                break;
            case END:
                asyncCount--;
                break;
        }
        loading.setVisibility(asyncCount > 0 ? Visibility.VISIBLE : Visibility.HIDDEN);
    }

    @ViewHandler
    public void back$click() {
        placeManager.goBack();
    }
    
    @ViewHandler
    public void goDefault$click() {
        placeManager.goDefault();
    }

    @ViewHandler
    public void forward$click() {
        placeManager.goForward();
    }

    @Override
    protected void initialize() {
        attacher.attach(getView());
    }

    @ViewInitializer
    protected void initializeView() {
        places.put("Login", placeManager.getToken(LoginPresenter.class));
        places.put("Dynamic", placeManager.getToken(DynamicPresenter.class));
        places.put("Complex blue", placeManager.getToken(ComplexPlacePresenter.class, new ComplexPlaceData("Hello", "blue")));
        places.put("Complex red", placeManager.getToken(ComplexPlacePresenter.class, new ComplexPlaceData("Goodbye", "red")));
        places.put("Microevents", placeManager.getToken(MicroeventsPresenter.class));
        places.put("Json Serializer", placeManager.getToken(JsonSerializerPresenter.class));
        places.put("Blips", placeManager.getToken(BlipPanel.class));

        loading.setVisibility(Visibility.HIDDEN);
    }

    private void render(int selected) {
        StringBuilder sb = new StringBuilder();
        int index = 0;
        for (Entry<String, String> entry : places.entrySet()) {
            sb.append("<a href='#");
            sb.append(entry.getValue());
            sb.append("'");
            if (selected == index) {
                sb.append(" style='font-weight:bold'");
            }
            sb.append(">");
            sb.append(entry.getKey());
            sb.append("</a><br/>");
            index++;
        }
        navegation.setInnerHTML(sb.toString());
    }

    public void setContent(View content, int selected) {
        panel.setWidget((Widget) content);
        render(selected);
    }
}
